//
//  ChannelToolBar.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/11.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ChannelToolBar.h"
#import "ChannelCellFrameViewModel.h"
#import "TAMacro.h"
#import "Masonry.h"
#import "ChannelContent.h"
#import "ChannelImg.h"
#import "ChannelTags.h"
#import "ChannelOfChannel.h"
@interface ChannelToolBar ()

@property (nonatomic, weak) UIImageView *sourceImage;
@property (nonatomic, weak) UILabel *sourceLabel;
@property (nonatomic, weak) UIButton *heartButton;
@property (nonatomic, weak) UILabel *likedNumLabel;
@property (nonatomic, weak) UIButton *commentButton;
@property (nonatomic, weak) UILabel *commentNumlabel;
@property (nonatomic, weak) UIButton *shareButton;
@property (nonatomic, weak) UIView *lineView;

@end



@implementation ChannelToolBar

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        // 创建子视图
        [self setUpAllChildViews];
    }
    return self;
}

// 创建子视图
- (void)setUpAllChildViews {
    
    // 来源图片
    UIImageView *sourceImage = [[UIImageView alloc] init];
    sourceImage.image = [UIImage imageNamed:@"postChannel"];
    [self addSubview:sourceImage];
    _sourceImage = sourceImage;
    
    // 来源Label
    UILabel *sourceLabel = [[UILabel alloc] init];
    sourceLabel.font = TagsFont;
    sourceLabel.textColor = [UIColor colorWithRed:97 / 255.0 green:221 / 255.0 blue:212 / 255.0 alpha:1];
    [self addSubview:sourceLabel];
    _sourceLabel = sourceLabel;
    
    // 点赞按钮
    UIButton *heartButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:heartButton];
    _heartButton = heartButton;


    // 点赞数量
    UILabel *likedNumlabel = [[UILabel alloc] init];
    likedNumlabel.font = TagsFont;
    likedNumlabel.textColor = [UIColor lightGrayColor];
    [self addSubview:likedNumlabel];
    _likedNumLabel = likedNumlabel;
    
    // 评论按钮
    UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [commentButton setImage:[UIImage imageNamed:@"channelComment"] forState:UIControlStateNormal];
    [self addSubview:commentButton];
    _commentButton = commentButton;

    // 评论数量
    UILabel *commentNumlabel = [[UILabel alloc] init];
    commentNumlabel.font = TagsFont;
    commentNumlabel.textColor = [UIColor lightGrayColor];
    [self addSubview:commentNumlabel];
    _commentNumlabel = commentNumlabel;
    
    // 分享按钮
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [shareButton setImage:[UIImage imageNamed:@"channelMore"] forState:UIControlStateNormal];
    [self addSubview:shareButton];
    _shareButton = shareButton;
    
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1];
    [self addSubview:lineView];
    _lineView = lineView;
    
    
    [self setUpFrame];

    
}

- (void)setChannelCellFrame:(ChannelCellFrameViewModel *)channelCellFrame {
    _channelCellFrame = channelCellFrame;
    // 赋frame
    // 赋数据
    [self setUpData];
}

- (void)setUpFrame {
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    [_sourceImage mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.width.height.mas_equalTo(20);
        make.left.mas_equalTo(10);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    [_sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(100);
        make.left.mas_equalTo(_sourceImage.mas_right).mas_equalTo(10);
        make.height.mas_equalTo(20);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    [_shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
        make.right.mas_equalTo(-10);
        make.centerY.mas_equalTo(self.mas_centerY);
        
    }];
    
    [_commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.mas_centerY);
        make.centerX.mas_equalTo(self.mas_centerX).multipliedBy(1.5);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    
    [_commentNumlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(20);
        make.left.mas_equalTo(self.commentButton.mas_right).mas_offset(2);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    [_heartButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.centerX.mas_equalTo(self.mas_centerX).multipliedBy(1.25);
        
    }];
    
    [_likedNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(20);
        make.left.mas_equalTo(self.heartButton.mas_right).mas_offset(2);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
}

- (void)setUpData {
    
    _sourceLabel.text = _channelCellFrame.channelContent.channel.content;
    if (_channelCellFrame.channelContent.likeNum != 0) {
        _likedNumLabel.text = [NSString stringWithFormat:@"%ld", _channelCellFrame.channelContent.likeNum];
    } else {
        _likedNumLabel.text = @"";
    }
    if (_channelCellFrame.channelContent.commentNum != 0) {
        _commentNumlabel.text = [NSString stringWithFormat:@"%ld", _channelCellFrame.channelContent.hotComments.count];
    } else {
        _commentNumlabel.text = @"";
    }
    if (_channelCellFrame.channelContent.isLiked == YES) {
        [_heartButton setImage:[UIImage imageNamed:@"liked"] forState:UIControlStateNormal];
    } else {
        [_heartButton setImage:[UIImage imageNamed:@"heart"] forState:UIControlStateNormal];
    }
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
